package com.anan.builder;

import com.anan.bean.TableInfo;
import com.anan.utils.StringTools;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class BuildController {

    public static void create(TableInfo tableInfo,String projectName) {
        String beanName = tableInfo.getBeanName();
        String beanNameb = StringTools.lowerCaseFirstLetter(beanName);
        String path = System.getProperty("user.dir") + "/" + projectName + "/src/main/java/com/anan/controller";
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (!mkdirs) {
                throw new RuntimeException("创建 " + path + " 目录失败");
            }
        }
        File beanFile = new File(path, beanName+"Controller.java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(beanFile);
            outw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outw);
            bw.write("package com.anan.controller;\n" +
                    "\n" +
                    "import com.anan.annotation.CheckObject;\n" +
                    "import com.anan.annotation.CheckValue;\n" +
                    "import com.anan.annotation.GlobalInterceptor;\n" +
                    "import com.anan.entity.dto."+beanName+"Dto;\n" +
                    "import com.anan.entity.po."+beanName+";\n" +
                    "import com.anan.entity.query."+beanName+"Query;\n" +
                    "import com.anan.exception.BusinessException;\n" +
                    "import com.anan.service."+beanName+"Service;\n" +
                    "import com.anan.utils.AnanUtils;\n" +
                    "import com.anan.utils.Result;\n" +
                    "import org.springframework.beans.BeanUtils;\n" +
                    "import org.springframework.web.bind.annotation.RequestBody;\n" +
                    "import org.springframework.web.bind.annotation.RequestMapping;\n" +
                    "import org.springframework.web.bind.annotation.RestController;\n" +
                    "\n" +
                    "import javax.annotation.Resource;\n" +
                    "import java.util.List;\n" +
                    "\n" +
                    "@RestController\n" +
                    "@RequestMapping(\"/"+tableInfo.getTableName()+"\")\n" +
                    "public class "+beanName+"Controller {\n" +
                    "\n" +
                    "    @Resource\n" +
                    "    "+beanName+"Service "+beanNameb+"Service;\n" +
                    "\n" +
                    "\n" +
                    "    @RequestMapping(\"/add\")\n" +
                    "    @GlobalInterceptor(checkParams = true, checkLogin = true)\n" +
                    "    public Result add(@CheckObject(required = true, checkProperties = {\"all\"}) @RequestBody "+beanName+" "+beanNameb+") {\n" +
                    "        Result result = new Result();\n" +
                    "\n" +
                    "        return result;\n" +
                    "    }\n" +
                    "\n" +
                    "    @RequestMapping(\"/delete\")\n" +
                    "    @GlobalInterceptor(checkParams = true, checkLogin = true)\n" +
                    "    public Result delete(@CheckObject(required = true, checkProperties = {\"all\"}) @RequestBody "+beanName+" "+beanNameb+") {\n" +
                    "        Result result = new Result();\n" +
                    "\n" +
                    "        return result;\n" +
                    "    }\n" +
                    "\n" +
                    "    @RequestMapping(\"/update\")\n" +
                    "    @GlobalInterceptor(checkParams = true, checkLogin = true)\n" +
                    "    public Result update(@CheckObject(required = true, checkProperties = {\"all\"}) @RequestBody "+beanName+" "+beanNameb+") {\n" +
                    "        Result result = new Result();\n" +
                    "\n" +
                    "        return result;\n" +
                    "    }\n" +
                    "\n" +
                    "    @RequestMapping(\"/get\")\n" +
                    "    @GlobalInterceptor(checkParams = true, checkLogin = true)\n" +
                    "    public Result get(@CheckObject(required = true, checkProperties = {\"all\"}) @RequestBody "+beanName+" "+beanNameb+") {\n" +
                    "        Result result = new Result();\n" +
                    "        return result;\n" +
                    "    }\n" +
                    "\n" +
                    "    @RequestMapping(\"/list\")\n" +
                    "    @GlobalInterceptor(checkParams = true, checkLogin = true)\n" +
                    "    public Result list(@CheckObject(required = true, checkProperties = {\"all\"}) @RequestBody "+beanName+" "+beanNameb+") {\n" +
                    "        Result result = new Result();\n" +
                    "        return result;\n" +
                    "    }\n" +
                    "\n" +
                    "    @RequestMapping(\"/listB\")\n" +
                    "    @GlobalInterceptor(checkParams = true, checkLogin = true)\n" +
                    "    public Result listB(@CheckObject(required = true, checkProperties = {\"all\"}) @RequestBody "+beanName+" "+ beanNameb +",\n" +
                    "                       @CheckObject(required = true, checkProperties = {\"all\"}) @RequestBody List<"+beanName+"> list) {\n" +
                    "        Result result = new Result();\n" +
                    "        return result;\n" +
                    "    }\n" +
                    "}\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}
